from flask import render_template, session, request, jsonify

from models import db
from models.index import Post, User, Comment, Category
from views import index_blu


@index_blu.route("/")
def index():
    # 获取post_id

    post_id = session.get('post_id', 1)
    user_id = session.get('user_id')
    nick_name = session.get('nick_name')

    post_m = db.session.query(Post).filter(Post.id == post_id).first()
    user = db.session.query(User).filter(User.id == user_id).first()
    post_all = db.session.query(Post).filter(Post.top == 1).all()
    post_status = [x.to_dict() for x in post_all]
    # 点击量
    clicks_top_6 = db.session.query(Post).filter().order_by(-Post.clicks).limit(6)
    # 创建时间
    time_Rank = db.session.query(Post).order_by(-Post.create_time).limit(6)
    # 评论
    if not post_m:
        return render_template('./index.html')
    comments = post_m.comments
    # 分类
    categorys = db.session.query(Category).filter(Category.id != 1).all()
    return render_template('index.html', clicks_top_6=clicks_top_6, post_m=post_m, user=user, time_Rank=time_Rank,
                           comments=comments, categorys=categorys, post_status=post_status)


@index_blu.route("/detail/<int:post_id>")
def detail(post_id):
    session['post_id'] = post_id
    user_id = session.get('user_id')
    user = db.session.query(User).filter(User.id == user_id).first()
    if not user:
        like_comment = []
    else:
        like_comment = user.like_comment
    post = db.session.query(Post).filter(Post.id == post_id).first()
    # 获取评论
    comments = post.comments.order_by(-Comment.create_time)
    time_Rank = db.session.query(Post).filter().order_by(-Post.create_time).limit(6)
    return render_template('./jie/detail.html', post=post, like_comment=like_comment, comments=comments, user=user,
                           user_id=user_id, time_Rank=time_Rank)


@index_blu.route('/postlist')
def postlist():
    user_id = session.get('user_id')
    post_id = session.get('post_id', 1)
    page = request.args.get('page', 1)  # 前端要的是哪一页的数据
    cid = int(request.args.get('cid', 0))  # 前端要的是哪个分类的数据，是股市、债市还是商品、外汇、公司
    per_page = request.args.get('per_page', 1)  # 前端要的是每一页中的新闻个数
    if cid == 1:
        paginate = db.session.query(Post).filter().order_by(-Post.create_time).paginate(
            page=int(page),
            per_page=int(
                per_page),
            error_out=False)
        post = db.session.query(Post).filter(Post.id == post_id).first()
    elif cid == 2:
        paginate = db.session.query(Post).filter(Post.c_status == 0).order_by(
            -Post.create_time).paginate(
            page=int(page), per_page=int(per_page), error_out=False)
    elif cid == 3:
        paginate = db.session.query(Post).filter(Post.c_status == 1).order_by(
            -Post.create_time).paginate(
            page=int(page), per_page=int(per_page), error_out=False)
    else:
        paginate = db.session.query(Post).filter(Post.best == 1).order_by(
            -Post.create_time).paginate(
            page=int(page), per_page=int(per_page), error_out=False)
    ret = {
        "totalPage": paginate.pages,  # 总页数
        "postList": [post.to_post_list_dict() for post in paginate.items],
    }
    return jsonify(ret)


# 提问
@index_blu.route('/jie/index')
def Questions():
    page = int(request.args.get("page", 1))
    user_id = session.get('user_id')
    post_id = session.get('post_id', 1)
    user = db.session.query(User).filter(User.id == user_id).first()
    post = db.session.query(Post).filter(Post.id == post_id).first()
    comments = post.comments.order_by(-Comment.create_time)
    clicks_top_6 = db.session.query(Post).filter(Post.category_id == 2).order_by(-Post.clicks).limit(6)
    time_Rank = db.session.query(Post).filter().order_by(-Post.create_time).limit(6)

    paginate = post.comments.paginate(page, 1, False)
    return render_template('./jie/index.html', clicks_top_6=clicks_top_6, comments=comments, time_Rank=time_Rank,
                           paginate=paginate, user=user, user_id=user_id)


@index_blu.route("/jie/index/<int:id>")
def Questions_id(id):
    page = int(request.args.get("page", 1))
    user_id = session.get('user_id')
    post_id = session.get('post_id', 1)
    user = db.session.query(User).filter(User.id == user_id).first()
    post = db.session.query(Post).filter(Post.id == post_id).first()
    comments = post.comments.order_by(-Comment.create_time)
    if id == 1:
        clicks_top_6 = db.session.query(Post).filter(Post.category_id == 2, Post.c_status == 0).order_by(
            -Post.clicks).limit(6)
        time_Rank = db.session.query(Post).filter().order_by(
            -Post.create_time).limit(6)
    elif id == 2:
        clicks_top_6 = db.session.query(Post).filter(Post.category_id == 2, Post.c_status == 1).order_by(
            -Post.clicks).limit(6)
        time_Rank = db.session.query(Post).filter().order_by(
            -Post.create_time).limit(6)
    else:
        clicks_top_6 = db.session.query(Post).filter(Post.category_id == 2, Post.best == 1).order_by(
            -Post.clicks).limit(6)
        time_Rank = db.session.query(Post).filter().order_by(
            -Post.create_time).limit(6)
    paginate = post.comments.paginate(page, 1, False)
    return render_template('./jie/index.html', clicks_top_6=clicks_top_6, comments=comments, time_Rank=time_Rank,
                           paginate=paginate, user=user, user_id=user_id)


# 分享
@index_blu.route('/jie/share')
def share():
    page = int(request.args.get("page", 1))
    user_id = session.get('user_id')
    post_id = session.get('post_id', 1)
    post = db.session.query(Post).filter(Post.id == post_id).first()
    user = db.session.query(User).filter(User.id == user_id).first()
    comments = post.comments.order_by(-Comment.create_time)
    clicks_top_6 = db.session.query(Post).filter(Post.category_id == 3).order_by(-Post.clicks).limit(6)
    time_Rank = db.session.query(Post).filter().order_by(-Post.create_time).limit(6)

    paginate = post.comments.paginate(page, 1, False)
    return render_template('./jie/share.html', clicks_top_6=clicks_top_6, comments=comments, time_Rank=time_Rank,
                           paginate=paginate, user=user, user_id=user_id)


@index_blu.route("/jie/share/<int:id>")
def share_id(id):
    page = int(request.args.get("page", 1))
    user_id = session.get('user_id')
    post_id = session.get('post_id', 1)
    user = db.session.query(User).filter(User.id == user_id).first()
    post = db.session.query(Post).filter(Post.id == post_id).first()
    comments = post.comments.order_by(-Comment.create_time)
    if id == 1:
        clicks_top_6 = db.session.query(Post).filter(Post.category_id == 3, Post.c_status == 0).order_by(
            -Post.clicks).limit(6)
        time_Rank = db.session.query(Post).filter().order_by(
            -Post.create_time).limit(6)
    elif id == 2:
        clicks_top_6 = db.session.query(Post).filter(Post.category_id == 3, Post.c_status == 1).order_by(
            -Post.clicks).limit(6)
        time_Rank = db.session.query(Post).filter().order_by(
            -Post.create_time).limit(6)
    else:
        clicks_top_6 = db.session.query(Post).filter(Post.category_id == 3, Post.best == 1).order_by(
            -Post.clicks).limit(6)
        time_Rank = db.session.query(Post).filter().order_by(
            -Post.create_time).limit(6)
    paginate = post.comments.paginate(page, 1, False)
    return render_template('./jie/share.html', clicks_top_6=clicks_top_6, comments=comments, time_Rank=time_Rank,
                           paginate=paginate, user=user, user_id=user_id)


#  讨论
@index_blu.route('/jie/discuss')
def discuss():
    page = int(request.args.get("page", 1))
    user_id = session.get('user_id')
    post_id = session.get('post_id', 1)
    post = db.session.query(Post).filter(Post.id == post_id).first()
    user = db.session.query(User).filter(User.id == user_id).first()
    comments = post.comments.order_by(-Comment.create_time)
    clicks_top_6 = db.session.query(Post).filter(Post.category_id == 4).order_by(-Post.clicks).limit(6)
    time_Rank = db.session.query(Post).filter().order_by(-Post.create_time).limit(6)

    paginate = post.comments.paginate(page, 1, False)
    return render_template('./jie/discuss.html', clicks_top_6=clicks_top_6, comments=comments, time_Rank=time_Rank,
                           paginate=paginate, user_id=user_id, user=user)


@index_blu.route("/jie/discuss/<int:id>")
def discuss_id(id):
    page = int(request.args.get("page", 1))
    user_id = session.get('user_id')
    post_id = session.get('post_id', 1)
    user = db.session.query(User).filter(User.id == user_id).first()
    post = db.session.query(Post).filter(Post.id == post_id).first()
    comments = post.comments.order_by(-Comment.create_time)
    if id == 1:
        clicks_top_6 = db.session.query(Post).filter(Post.category_id == 4, Post.c_status == 0).order_by(
            -Post.clicks).limit(6)
        time_Rank = db.session.query(Post).filter().order_by(
            -Post.create_time).limit(6)
    elif id == 2:
        clicks_top_6 = db.session.query(Post).filter(Post.category_id == 4, Post.c_status == 1).order_by(
            -Post.clicks).limit(6)
        time_Rank = db.session.query(Post).filter().order_by(
            -Post.create_time).limit(6)
    else:
        clicks_top_6 = db.session.query(Post).filter(Post.category_id == 4, Post.best == 1).order_by(
            -Post.clicks).limit(6)
        time_Rank = db.session.query(Post).filter().order_by(
            -Post.create_time).limit(6)
    paginate = post.comments.paginate(page, 1, False)
    return render_template('./jie/discuss.html', clicks_top_6=clicks_top_6, comments=comments, time_Rank=time_Rank,
                           paginate=paginate, user=user, user_id=user_id)


#  建议
@index_blu.route('/jie/suggest')
def suggest():
    page = int(request.args.get("page", 1))
    user_id = session.get('user_id')
    post_id = session.get('post_id', 1)
    post = db.session.query(Post).filter(Post.id == post_id).first()
    user = db.session.query(User).filter(User.id == user_id).first()
    comments = post.comments.order_by(-Comment.create_time)
    clicks_top_6 = db.session.query(Post).filter(Post.category_id == 5).order_by(-Post.clicks).limit(6)
    time_Rank = db.session.query(Post).filter().order_by(-Post.create_time).limit(6)

    paginate = post.comments.paginate(page, 1, False)
    return render_template('./jie/suggest.html', clicks_top_6=clicks_top_6, comments=comments, time_Rank=time_Rank,
                           paginate=paginate, user=user, user_id=user_id)


@index_blu.route("/jie/suggest/<int:id>")
def suggest_id(id):
    page = int(request.args.get("page", 1))
    user_id = session.get('user_id')
    post_id = session.get('post_id', 1)
    user = db.session.query(User).filter(User.id == user_id).first()
    post = db.session.query(Post).filter(Post.id == post_id).first()
    comments = post.comments.order_by(-Comment.create_time)
    if id == 1:
        clicks_top_6 = db.session.query(Post).filter(Post.category_id == 5, Post.c_status == 0).order_by(
            -Post.clicks).limit(6)
        time_Rank = db.session.query(Post).filter().order_by(
            -Post.create_time).limit(6)
    elif id == 2:
        clicks_top_6 = db.session.query(Post).filter(Post.category_id == 5, Post.c_status == 1).order_by(
            -Post.clicks).limit(6)
        time_Rank = db.session.query(Post).filter().order_by(
            -Post.create_time).limit(6)
    else:
        clicks_top_6 = db.session.query(Post).filter(Post.category_id == 5, Post.best == 1).order_by(
            -Post.clicks).limit(6)
        time_Rank = db.session.query(Post).filter().order_by(
            -Post.create_time).limit(6)
    paginate = post.comments.paginate(page, 1, False)
    return render_template('./jie/suggest.html', clicks_top_6=clicks_top_6, comments=comments, time_Rank=time_Rank,
                           paginate=paginate, user=user, user_id=user_id)


#  公告
@index_blu.route('/jie/notice')
def notice():
    page = int(request.args.get("page", 1))
    user_id = session.get('user_id')
    post_id = session.get('post_id', 1)
    post = db.session.query(Post).filter(Post.id == post_id).first()
    user = db.session.query(User).filter(User.id == user_id).first()
    comments = post.comments.order_by(-Comment.create_time)
    clicks_top_6 = db.session.query(Post).filter(Post.category_id == 6).order_by(-Post.clicks).limit(6)
    time_Rank = db.session.query(Post).filter().order_by(-Post.create_time).limit(6)

    paginate = post.comments.paginate(page, 1, False)

    return render_template('./jie/notice.html', clicks_top_6=clicks_top_6, comments=comments, time_Rank=time_Rank,
                           paginate=paginate, user=user, user_id=user_id)


@index_blu.route("/jie/notice/<int:id>")
def notice_id(id):
    page = int(request.args.get("page", 1))
    user_id = session.get('user_id')
    post_id = session.get('post_id', 1)
    user = db.session.query(User).filter(User.id == user_id).first()
    post = db.session.query(Post).filter(Post.id == post_id).first()
    comments = post.comments.order_by(-Comment.create_time)
    if id == 1:
        clicks_top_6 = db.session.query(Post).filter(Post.category_id == 6, Post.c_status == 0).order_by(
            -Post.clicks).limit(6)
        time_Rank = db.session.query(Post).filter().order_by(
            -Post.create_time).limit(6)
    elif id == 2:
        clicks_top_6 = db.session.query(Post).filter(Post.category_id == 6, Post.c_status == 1).order_by(
            -Post.clicks).limit(6)
        time_Rank = db.session.query(Post).filter().order_by(
            -Post.create_time).limit(6)
    else:
        clicks_top_6 = db.session.query(Post).filter(Post.category_id == 6, Post.best == 1).order_by(
            -Post.clicks).limit(6)
        time_Rank = db.session.query(Post).filter().order_by(
            -Post.create_time).limit(6)
    paginate = post.comments.paginate(page, 1, False)
    return render_template('./jie/notice.html', clicks_top_6=clicks_top_6, comments=comments, time_Rank=time_Rank,
                           paginate=paginate, user=user, user_id=user_id)


#  动态
@index_blu.route('/jie/dynamic')
def dynamic():
    page = int(request.args.get("page", 1))
    user_id = session.get('user_id')
    post_id = session.get('post_id', 1)
    post = db.session.query(Post).filter(Post.id == post_id).first()
    user = db.session.query(User).filter(User.id == user_id).first()
    comments = post.comments.order_by(-Comment.create_time)
    clicks_top_6 = db.session.query(Post).filter(Post.category_id == 7).order_by(-Post.clicks).limit(6)
    time_Rank = db.session.query(Post).filter().order_by(-Post.create_time).limit(6)

    paginate = post.comments.paginate(page, 1, False)

    return render_template('./jie/dynamic.html', clicks_top_6=clicks_top_6, comments=comments, time_Rank=time_Rank,
                           paginate=paginate, user_id=user_id, user=user)


@index_blu.route("/jie/dynamic/<int:id>")
def dynamic_id(id):
    page = int(request.args.get("page", 1))
    user_id = session.get('user_id')
    post_id = session.get('post_id', 1)
    user = db.session.query(User).filter(User.id == user_id).first()
    post = db.session.query(Post).filter(Post.id == post_id).first()
    comments = post.comments.order_by(-Comment.create_time)
    if id == 1:
        clicks_top_6 = db.session.query(Post).filter(Post.category_id == 7, Post.c_status == 0).order_by(
            -Post.clicks).limit(6)
        time_Rank = db.session.query(Post).filter().order_by(
            -Post.create_time).limit(6)
    elif id == 2:
        clicks_top_6 = db.session.query(Post).filter(Post.category_id == 7, Post.c_status == 1).order_by(
            -Post.clicks).limit(6)
        time_Rank = db.session.query(Post).filter().order_by(
            -Post.create_time).limit(6)
    else:
        clicks_top_6 = db.session.query(Post).filter(Post.category_id == 7, Post.best == 1).order_by(
            -Post.clicks).limit(6)
        time_Rank = db.session.query(Post).filter().order_by(
            -Post.create_time).limit(6)
    paginate = post.comments.paginate(page, 1, False)
    return render_template('./jie/dynamic.html', clicks_top_6=clicks_top_6, comments=comments, time_Rank=time_Rank,
                           paginate=paginate, user=user, user_id=user_id)


# 发表
@index_blu.route('/jie/add', methods=['POST', 'GET'])
def add():
    method = request.method
    if method == 'GET':
        user_id = session.get('user_id')
        user = db.session.query(User).filter(User.id == user_id).first()
        if not user:
            return render_template('./user/login.html')
        return render_template('./jie/add.html', user=user)

    else:
        user_id = session.get("user_id")
        user = db.session.query(User).filter(User.id == user_id).first()
        title = request.form.get('title')  # 标题
        category = request.form.get('class')  # 分类
        content = request.form.get('content')  # 内容
        fly_kiss = request.form.get('experience')  # 悬赏
        post = Post()
        post.title = title
        post.category_id = category
        post.content = content
        post.fly_kiss = fly_kiss
        post.user_id = user_id
        post.source = user.nick_name
        db.session.add(post)
        db.session.commit()
        ret = {
            "errno": 0,
            "errmsg": "成功"
        }

        return jsonify(ret)


@index_blu.route('/jie/delete', methods=['POST'])
def delete():
    post_id = request.form.get('post_id')
    user_id = session.get('user_id')
    user = db.session.query(User).filter(User.id == user_id, User.is_admin == 1).first()
    if not user:
        ret = {
            'status': 4000,
            "msg": "请登录或者你不是管理员"
        }
        return jsonify(ret)
    if user:
        try:
            # 删除
            post = db.session.query(Post).filter(Post.id == post_id).first()
            db.session.delete(post)
            db.session.commit()
            ret = {
                'status': 0,
                "msg": "删除成功"
            }

            return jsonify(ret)

        except Exception as ret:
            db.session.rollback()
            ret = {
                "status": 4001,
                "msg": "删除失败"
            }

            return jsonify(ret)


@index_blu.route('/jie/set', methods=['POST'])
def set_top():
    post_id = request.form.get('post_id')
    rank = request.form.get('rank')
    # post_id = request.form.get('field')
    user_id = session.get('user_id')
    user = db.session.query(User).filter(User.id == user_id, User.is_admin == 1).first()
    if not user:
        ret = {
            'status': 4000,
            "msg": "请登录或者你不是管理员"
        }
        return jsonify(ret)

    if user:
        # 置顶
        if rank == '1':
            post = db.session.query(Post).filter(Post.id == post_id).first()
            post.top = 1
            db.session.commit()
            ret = {
                'status': 0,
                "msg": "置顶成功"
            }
            return jsonify(ret)

        else:
            post = db.session.query(Post).filter(Post.id == post_id).first()
            post.best = 1
            db.session.commit()
            ret = {
                'status': 0,
                "msg": "加精成功"
            }
            return jsonify(ret)